package com.kexio.security.dto;

import com.kexio.common.dto.PageRequest;
import com.kexio.security.domain.enums.UserStatus;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 用户请求DTO（统一请求对象）
 * 
 * @author Kexio Team
 * @since 1.0.0
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Schema(description = "用户请求DTO")
public class UserRequest extends PageRequest {

    // ==================== 验证分组 ====================
    public interface Create {}
    public interface Update {}
    public interface Query {}

    /**
     * 用户ID
     */
    @NotNull(groups = Update.class, message = "用户ID不能为空")
    @Schema(description = "用户ID", example = "1")
    private Long id;

    /**
     * 用户名
     */
    @NotBlank(groups = {Create.class}, message = "用户名不能为空")
    @Size(min = 3, max = 50, groups = {Create.class, Update.class}, message = "用户名长度必须在3-50之间")
    @Pattern(regexp = "^[a-zA-Z0-9_]+$", groups = {Create.class, Update.class}, 
             message = "用户名只能包含字母、数字和下划线")
    @Schema(description = "用户名", example = "admin")
    private String username;

    /**
     * 密码
     */
    @NotBlank(groups = Create.class, message = "密码不能为空")
    @Size(min = 6, max = 32, groups = {Create.class, Update.class}, message = "密码长度必须在6-32之间")
    @Schema(description = "密码", example = "password123")
    private String password;

    /**
     * 昵称
     */
    @Size(max = 50, groups = {Create.class, Update.class}, message = "昵称长度不能超过50")
    @Schema(description = "昵称", example = "管理员")
    private String nickname;

    /**
     * 真实姓名
     */
    @Size(max = 50, groups = {Create.class, Update.class}, message = "真实姓名长度不能超过50")
    @Schema(description = "真实姓名", example = "张三")
    private String realName;

    /**
     * 邮箱
     */
    @Email(groups = {Create.class, Update.class}, message = "邮箱格式不正确")
    @Size(max = 100, groups = {Create.class, Update.class}, message = "邮箱长度不能超过100")
    @Schema(description = "邮箱", example = "admin@kexio.com")
    private String email;

    /**
     * 手机号
     */
    @Pattern(regexp = "^1[3-9]\\d{9}$", groups = {Create.class, Update.class}, 
             message = "手机号格式不正确")
    @Schema(description = "手机号", example = "13800138000")
    private String phone;

    /**
     * 头像URL
     */
    @Size(max = 500, groups = {Create.class, Update.class}, message = "头像URL长度不能超过500")
    @Schema(description = "头像URL", example = "https://example.com/avatar.jpg")
    private String avatar;

    /**
     * 用户状态
     */
    @Schema(description = "用户状态", example = "1")
    private Integer status;

    /**
     * 部门ID
     */
    @Schema(description = "部门ID", example = "1")
    private Long deptId;

    /**
     * 岗位ID
     */
    @Schema(description = "岗位ID", example = "1")
    private Long positionId;

    /**
     * 备注
     */
    @Size(max = 500, groups = {Create.class, Update.class}, message = "备注长度不能超过500")
    @Schema(description = "备注", example = "系统管理员")
    private String remark;

    // ==================== 查询条件字段 ====================

    /**
     * 用户名（模糊查询）
     */
    @Schema(description = "用户名（模糊查询）", example = "admin")
    private String usernameKeyword;

    /**
     * 昵称（模糊查询）
     */
    @Schema(description = "昵称（模糊查询）", example = "管理")
    private String nicknameKeyword;

    /**
     * 邮箱（模糊查询）
     */
    @Schema(description = "邮箱（模糊查询）", example = "@kexio.com")
    private String emailKeyword;

    /**
     * 状态列表（多选查询）
     */
    @Schema(description = "状态列表", example = "[1, 3]")
    private java.util.List<Integer> statusList;

    /**
     * 部门ID列表（多选查询）
     */
    @Schema(description = "部门ID列表", example = "[1, 2, 3]")
    private java.util.List<Long> deptIdList;

    // ==================== 静态工厂方法 ====================

    /**
     * 创建用户请求
     * @return 用户请求对象
     */
    public static UserRequest createRequest() {
        return new UserRequest();
    }

    /**
     * 更新用户请求
     * @param id 用户ID
     * @return 用户请求对象
     */
    public static UserRequest updateRequest(Long id) {
        UserRequest request = new UserRequest();
        request.setId(id);
        return request;
    }

    /**
     * 查询用户请求
     * @return 用户请求对象
     */
    public static UserRequest queryRequest() {
        return new UserRequest();
    }

    // ==================== 便利方法 ====================

    /**
     * 获取用户状态枚举
     * @return 用户状态枚举
     */
    public UserStatus getUserStatus() {
        return UserStatus.fromCode(this.status);
    }

    /**
     * 设置用户状态
     * @param userStatus 用户状态枚举
     */
    public void setUserStatus(UserStatus userStatus) {
        this.status = userStatus != null ? userStatus.getCode() : null;
    }

    /**
     * 检查是否为创建操作
     * @return 是否创建操作
     */
    public boolean isCreateOperation() {
        return this.id == null;
    }

    /**
     * 检查是否为更新操作
     * @return 是否更新操作
     */
    public boolean isUpdateOperation() {
        return this.id != null;
    }

    // ==================== 手写Setter方法 ====================
    
    public void setId(Long id) { this.id = id; }
    public void setUsername(String username) { this.username = username; }
    public void setPassword(String password) { this.password = password; }
    public void setNickname(String nickname) { this.nickname = nickname; }
    public void setRealName(String realName) { this.realName = realName; }
    public void setEmail(String email) { this.email = email; }
    public void setPhone(String phone) { this.phone = phone; }
    public void setAvatar(String avatar) { this.avatar = avatar; }
    public void setStatus(Integer status) { this.status = status; }
    public void setDeptId(Long deptId) { this.deptId = deptId; }
    public void setPositionId(Long positionId) { this.positionId = positionId; }
    public void setRemark(String remark) { this.remark = remark; }
    public void setUsernameKeyword(String usernameKeyword) { this.usernameKeyword = usernameKeyword; }
    public void setNicknameKeyword(String nicknameKeyword) { this.nicknameKeyword = nicknameKeyword; }
    public void setEmailKeyword(String emailKeyword) { this.emailKeyword = emailKeyword; }
    public void setStatusList(java.util.List<Integer> statusList) { this.statusList = statusList; }
    public void setDeptIdList(java.util.List<Long> deptIdList) { this.deptIdList = deptIdList; }

    // ==================== 手写Getter方法 ====================
    
    public Long getId() { return id; }
    public String getUsername() { return username; }
    public String getPassword() { return password; }
    public String getNickname() { return nickname; }
    public String getRealName() { return realName; }
    public String getEmail() { return email; }
    public String getPhone() { return phone; }
    public String getAvatar() { return avatar; }
    public Integer getStatus() { return status; }
    public Long getDeptId() { return deptId; }
    public Long getPositionId() { return positionId; }
    public String getRemark() { return remark; }
    public String getUsernameKeyword() { return usernameKeyword; }
    public String getNicknameKeyword() { return nicknameKeyword; }
    public String getEmailKeyword() { return emailKeyword; }
    public java.util.List<Integer> getStatusList() { return statusList; }
    public java.util.List<Long> getDeptIdList() { return deptIdList; }
}